package git.soulbgm.common.mapper;

import org.apache.ibatis.mapping.MappedStatement;
import tk.mybatis.mapper.entity.EntityColumn;
import tk.mybatis.mapper.mapperhelper.EntityHelper;
import tk.mybatis.mapper.mapperhelper.MapperHelper;
import tk.mybatis.mapper.mapperhelper.MapperTemplate;
import tk.mybatis.mapper.mapperhelper.SqlHelper;

import java.util.Iterator;
import java.util.Set;

/**
 * Oracle自动生成批量插入代码
 *
 * @author SoulBGM
 * @version V1.0
 * @date 2018-10-12 20:13
 */
public class OracleProvider extends MapperTemplate {
    public OracleProvider(Class<?> mapperClass, MapperHelper mapperHelper) {
        super(mapperClass, mapperHelper);
    }

    public String insertList(MappedStatement ms) {
        Class<?> entityClass = this.getEntityClass(ms);

        StringBuilder sql = new StringBuilder();
        sql.append(SqlHelper.insertIntoTable(entityClass, this.tableName(entityClass)));

        sql.append(SqlHelper.insertColumns(entityClass, false, false, false));

        sql.append("<foreach close=\")\" collection=\"list\" item=\"record\" open=\"(\" separator=\"union\">");
        sql.append(" SELECT ");
        sql.append("<trim suffixOverrides=\",\">");
        Set<EntityColumn> columnList = EntityHelper.getColumns(entityClass);
        Iterator var5 = columnList.iterator();

        while (var5.hasNext()) {
            EntityColumn column = (EntityColumn) var5.next();
            if (column.isInsertable()) {
                sql.append(column.getColumnHolder("record") + ",");
            }
        }

        sql.append("</trim>");
        sql.append(" FROM DUAL ");
        sql.append("</foreach>");
        return sql.toString();
    }

}
